package com.ynny.practice5_2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import java.util.List;

@Component(value = "userManage")
public class UderManageImpl implements UderManage{
    @Autowired
    JdbcTemplate jdbcTemplate;


    @Override
    public boolean login(User user) {
        String sql = "select * from user where number = ? and password = ? and identity = ?";
        RowMapper<User> rowMapper = new BeanPropertyRowMapper<>(User.class);
        List<User> users = jdbcTemplate.query(sql, new Object[]{user.getNumber(), user.getPassword(), user.getIdentity()}, rowMapper);
        return !((List<?>) users).isEmpty();
    }
    @Override
    public List<User> all() {
        String sql = "select * from user";
        RowMapper<User> rowMapper ;
        rowMapper = new BeanPropertyRowMapper<User>(User.class);
        List<User> list = jdbcTemplate.query(sql,rowMapper);
        return list;
    }

    @Override
    public void quit() {
        System.out.println("退出登录");
    }
}
